package com.gamecauca.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.gamecauca.Assets;
import com.gamecauca.Input;
import com.gamecauca.Settings;
import com.gamecauca.common.Constant;

public class MainMenuScreen extends Screen {
	OrthographicCamera guiCam;
	SpriteBatch batcher;
	
	public boolean flg = false;
	public int xM, yM; 
	public int xO, yO;

	public MainMenuScreen (Game game) {
		super(game);
		this.game.setNumSreen(Constant.MainMenuScreen); 
		guiCam = new OrthographicCamera(Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT);
		guiCam.position.set(Constant.SCREEN_WIDTH/ 2, Constant.SCREEN_HEIGHT / 2, 0);
		batcher = new SpriteBatch();
		
	}
	
	public void tick(Input input) {
//		System.out.println("MainMenuScreen:--------------------- loc oi la loc");
		//System.out.println("Count: " + count);
		if(xM == (int)((Gdx.input.getX(0) / (float)Gdx.graphics.getWidth()) *800))
			xM = 0;
		else{
			
			if(xO == (int)((Gdx.input.getX(0) / (float)Gdx.graphics.getWidth()) *800))
				xM = 0;
			else
			{
				xM = (int)((Gdx.input.getX(0) / (float)Gdx.graphics.getWidth()) *800);
				xO = xM;
			}
		}
		
		if(yM == (int)((Gdx.input.getY(0) / (float)Gdx.graphics.getHeight()) * 480))
			yM = 0;
		else
		{
			if(yO == (int)((Gdx.input.getY(0) / (float)Gdx.graphics.getHeight()) * 480))
				yM = 0;
			else
			{
				yM = (int)((Gdx.input.getY(0) / (float)Gdx.graphics.getHeight()) * 480);
				yO = yM;
			}			
		}
	
		if(flg)
		{
			if(Constant.checkCollision(xM, yM, 207, 248, 371, 328)){				
				flg = false;
			}
			else if(Constant.checkCollision(xM, yM, 700, 376, 780, 440)){
				flg = false;
			}
			else if(Constant.checkCollision(xM, yM, 620, 360, 680, 400)){
				flg = false;
			}
				
		}
				
		if(input.buttons[Input.PLAYGAME]){
//			batcher.begin();
////			batcher.draw(Assets.PlayButtonsRegion[1], 220, 230);
//			batcher.end();
			
			//batcher.dispose();
			this.game.setScreen(new GameScreen(game, this));
//			this.game.setScreen(new ChooseModeScreen(game, this));
			input.releaseAllKeys();
			return;
			
		}
		else if(input.buttons[Input.EXITGAME]){
			Gdx.app.exit();
			return;
		}
		else if(Constant.checkCollision(xM, yM, 207, 248, 371, 328) && !flg)
		{
			if(GameCauCa.actionResolver != null)
			{
				GameCauCa.actionResolver.boot();
				
				flg = true; 
				return;
			}
		}
		else if(input.buttons[Input.CREDITS])
		{
//			GameCauCa.actionResolver.showScoreDialog();
			this.game.setScreen(new CatalogScreen(game));
			input.releaseAllKeys();
			return;
		}
		else if(Constant.checkCollision(xM, yM, 700, 376, 780, 440) && !flg){
			GameCauCa.actionResolver.showFacebook();
			input.releaseAllKeys();
			return;
		}
		else if(Constant.checkCollision(xM, yM, 620, 360, 680, 400) && !flg){
			GameCauCa.actionResolver.showTwitter();
			input.releaseAllKeys();
			return;
		}
		
	}
	
	@Override
	public void present () {
//		GLCommon gl = Gdx.gl;
//		gl.glClearColor(1, 0, 0, 1);
//		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		guiCam.update();
		batcher.setProjectionMatrix(guiCam.combined);

//		batcher.disableBlending();
		batcher.begin();
		batcher.draw(Assets.mainMenuRegion, 0, 0);
		batcher.draw(Assets.PlayButtonsRegion[0], 220, 230);
		batcher.draw(Assets.loadGameButtonsRegion[0], 435, 225);
		batcher.draw(Assets.hightscoreButtonsRegion[0], 220, 120);
		batcher.draw(Assets.helpButtonsRegion[0], 430, 120);
		batcher.draw(Assets.creditButtonsRegion[0], 220, 30);
		batcher.draw(Assets.quitButtonsRegion[0], 430, 30);
		batcher.draw(Assets.facebookTexture, Constant.SCREEN_WIDTH - 80, 40);
		batcher.draw(Assets.twitterTexture, Constant.SCREEN_WIDTH - 170, 40);
		batcher.end();
		

	}

	@Override
	public void pause () {
		Settings.save();
	}

	@Override
	public void resume () {
	}

	@Override
	public void dispose () {
	}
}

